package com.ebupt.migu.fineoperationactivitymonitor.common.util;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

/**
 * 人群重组类
 * 
 * @author zhanggk
 */
public class Recombination {

    /** 分隔符逗号 */
    private static final String SEPARATOR_COMMA = ",";

    /** 分隔符竖线 */
    private static final String SEPARATOR_VERTICAL_LINE = "\\|";

    /** 占位符下划线 */
    private static final String PLACEHOLDER_UNDERSCORE = "_";

    /**
     * 交集
     */
    public static Set<String> intersection(String userStr1, String userStr2) {
        Set<String> set = new HashSet<>();
        if (StringUtils.isNotBlank(userStr1) && StringUtils.isNotBlank(userStr2)) {
            String[] user1 = userStr1.split(SEPARATOR_VERTICAL_LINE);
            String[] user2 = userStr2.split(SEPARATOR_VERTICAL_LINE);
            for (String u1 : user1) {
                for (String u2 : user2) {
                    if (u1.equals(u2)) {
                        set.add(u1);
                        break;
                    }
                    String[] userKey1 = u1.split(SEPARATOR_COMMA);
                    String[] userKey2 = u2.split(SEPARATOR_COMMA);
                    if (equalsUserKey(userKey1[0], userKey2[0])) {
                        set.add(u1);
                        set.add(u2);
                        break;
                    }
                    if (equalsUserKey(userKey1[1], userKey2[1])) {
                        set.add(u1);
                        set.add(u2);
                        break;
                    }
                    if (equalsUserKey(userKey1[2], userKey2[2])) {
                        set.add(u1);
                        set.add(u2);
                        break;
                    }
                }
            }
        }
        return set;
    }

    /**
     * 并集
     */
    public static Set<String> union(String userStr1, String userStr2) {
        Set<String> set = new HashSet<>();
        if (StringUtils.isNotBlank(userStr1) && StringUtils.isNotBlank(userStr2)) {
            String[] user1 = userStr1.split(SEPARATOR_VERTICAL_LINE);
            String[] user2 = userStr2.split(SEPARATOR_VERTICAL_LINE);
            Set<String> u1 = new HashSet<>(Arrays.asList(user1));
            Set<String> u2 = new HashSet<>(Arrays.asList(user2));
            u1.addAll(u2);
            return u1;
        }
        return set;
    }

    /**
     * 左差集
     */
    public static Set<String> leftDifference(String userStr1, String userStr2) {
        Set<String> set = new HashSet<>();
        if (StringUtils.isNotBlank(userStr1) && StringUtils.isNotBlank(userStr2)) {
            String[] user1 = userStr1.split(SEPARATOR_VERTICAL_LINE);
            String[] user2 = userStr2.split(SEPARATOR_VERTICAL_LINE);
            for (String u1 : user1) {
                boolean bl = true;
                for (String u2 : user2) {
                    if (u1.equals(u2)) {
                        bl = false;
                        break;
                    }
                    String[] userKey1 = u1.split(SEPARATOR_COMMA);
                    String[] userKey2 = u2.split(SEPARATOR_COMMA);
                    if (equalsUserKey(userKey1[0], userKey2[0])) {
                        bl = false;
                        break;
                    }
                    if (equalsUserKey(userKey1[1], userKey2[1])) {
                        bl = false;
                        break;
                    }
                    if (equalsUserKey(userKey1[2], userKey2[2])) {
                        bl = false;
                        break;
                    }
                }
                if (bl) {
                    set.add(u1);
                }
            }
        }
        return set;
    }

    /**
     * 右差集
     */
    public static Set<String> rightDifference(String userStr1, String userStr2) {
        return leftDifference(userStr2, userStr1);
    }

    /**
     * 对称差集
     */
    public static Set<String> symmetricDifference(String userStr1, String userStr2) {
        Set<String> set1 = leftDifference(userStr1, userStr2);
        Set<String> set2 = rightDifference(userStr1, userStr2);
        set1.addAll(set2);
        return set1;
    }

    /**
     * 对比字段
     * 
     * @param userKey1 字段1
     * @param userKey2 字段2
     * @return 字符不为空，不为空串，不为竖线，且字段一和字段二 一致
     */
    private static boolean equalsUserKey(String userKey1, String userKey2) {
        return StringUtils.isNotBlank(userKey1) && !userKey1.equals(PLACEHOLDER_UNDERSCORE)
            && StringUtils.isNotBlank(userKey2) && !userKey2.equals(PLACEHOLDER_UNDERSCORE)
            && userKey1.equals(userKey2);
    }

    //
    // public static void main(String[] args) {
    // String user1 = "a,b,c|d,e,f|e,_,_";
    // String user2 = "a,e,f|g,h,i";
    //
    // System.out.println("交集：" + intersection(user1, user2).toString());
    //
    // System.out.println("并集：" + union(user1, user2).toString());
    //
    // System.out.println("左差集：" + leftDifference(user1, user2).toString());
    //
    // System.out.println("右差集：" + rightDifference(user1, user2).toString());
    //
    // System.out.println("对称差集：" + symmetricDifference(user1, user2).toString());
    //
    // }

}
